﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmCostTypeManager : FrmMDIChildFormTemplate
    {
        private CostTypeController _controller = new CostTypeController();
        private tb_CostType _costType = null;
        private static FrmCostTypeManager instance = null;
        private FrmCostTypeManager()
        {
            InitializeComponent();
        }


        public static FrmCostTypeManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmCostTypeManager();
                }
                return instance;
            }
        }

        private void FrmCostTypeManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlPriceType;
            _controller.View = gridViewPriceType;
            _controller.GetData();
        }

        private void gridViewPriceType_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _costType = (tb_CostType)gridViewPriceType.GetFocusedRow();
            if (_costType != null)
            {
                txtRemark.Text = _costType.Remark;
                txtTypeName.Text = _costType.TypeName;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_CostType cost = new tb_CostType();
                cost.Remark = txtRemark.Text;
                cost.TypeName = txtTypeName.Text;
                _controller.Add(cost);

                //link datagrid
                _controller.Grid = gridControlPriceType;
                _controller.View = gridViewPriceType;
                _controller.GetData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtTypeName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_CostType cost = _controller.GetCostTypeObj(_costType.CostTypeId);
                cost.Remark = txtRemark.Text;
                cost.TypeName = txtTypeName.Text;
                _controller.Update();


                //refresh datagrid
                _controller.Grid = gridControlPriceType;
                _controller.View = gridViewPriceType;
                _controller.GetData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                tb_CostType  cost = _controller.GetCostTypeObj(_costType.CostTypeId);
                _controller.Delete(cost);

                //refresh datagrid
                _controller.Grid = gridControlPriceType;
                _controller.View = gridViewPriceType;
                _controller.GetData();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtTypeName.Enabled = false;
            txtRemark.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }


        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtTypeName.Text == string.Empty)
            {
                txtTypeName.Focus();
                MessageBox.Show("计价类型名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtRemark.Text == string.Empty)
            {
                txtRemark.Focus();
                MessageBox.Show("备注不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtTypeName.Enabled = bl;
            txtRemark.Enabled = bl;

            txtTypeName.Text = string.Empty;
            txtRemark.Text = string.Empty;
            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtTypeName.Enabled = bl;
            txtRemark.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmCostTypeManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }


    }
}
